import { defineStore } from "pinia";

export const useRoleListStore = defineStore("useRoleListStore", {
	state: () => ({
		roleList: [],
		idMapToRole: new Map(),
	}),
	actions: {
		initialize() {
			
		},
		buildMap() {
			if (this.idMapToRole.size === 0) {
				this.roleList.forEach((el) => {
					this.idMapToRole.set(el.value, el);
				});
			}
		},
		getRoleSlice(ids: string): string[] {
			const ans = [] as string[];
			const m = this.idMapToRole;

			for (const [id, el] of m) {
				if (ids.includes(id)) {
					ans.push(el);
				}
			}
			return ans;
		},
		getRoleNameFromIds(ids: string): string[] {
			this.buildMap();
			return this.getRoleSlice(ids).map((el) => el.label);
		},
	},
});

// export interface DistrictSelectChild {
//   children: DistrictSelectChild[];
//   districtCode: string;
//   districtName: string;
//   parentDistrictCode: string;
// }

// export const useDistrict = defineStore(
//   "userDistrictList",
//   {
//     state: () => ({ districtList: [], idMapToDistrict: new Map() }),
//     actions: {
//       async initialize() {
//         const rs = await userService.getDistrictList<ServiceStructure>();
//         if (rs.ok) {
//           this.districtList = rs.data;
//         }
//       },
//       getChildFromIds(paths: string): DistrictSelectChild {
//         let list = this.districtList;

//         const pathStack = paths.split(",");
//         let ans = null;
//         let stack = [list];

//         while (stack.length > 0) {
//           const currentId = pathStack[0];
//           pathStack.shift();
//           const currentList = stack.pop() as DistrictSelectChild[];

//           ans = currentList?.find(el => el.districtCode === currentId);

//           if (pathStack.length === 0 || typeof ans === "undefined") {
//             break;
//           }
//           stack.push(ans.children);
//         }
//         return ans;
//       },
//       buildMap() {
//         // todo: 构建地区map
//       }
//     }
//   }
// );
